/* 
 * Copyright [2018] [Alex/libo(liboms@hotmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.guava.object;


import java.io.IOException;
import java.util.stream.Stream;

import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.base.Optional;
import com.google.common.base.Throwables;
import com.hyts.aop.annotation.Aspect;

/** 
 * @title ObjectUtils.java  
 * @package com.hyts.guava.optional  
 * @description 对象操作工具类 
 * 				Guava框架进行判断Object对象是否为空<br>
 * 				对象的相关信息是否为规则要求的数据信息<br>
 * 				作者在此方向对Guava的Object进行一层的<br>
 * 				注意：JDK7 引入的 Objects<br>
 * @see com.google.common.base.Objects
 * 		com.google.common.base.Optional
 * @author libo/Alex
 * @email liboms@hotmail.com/liboware@163.com  
 * @date 2018年5月4日 
 * @version V1.0 
 */
public final class Objects {
	
	/**  
	 * <b>TODO(方法功能的描述)</b>
	 * <p>TODO(这里用一句话描述这个方法的作用)</p>
	 * @param param
	 * @exception
	 */ 
	public static void checkNull(Object param){Optional.of(param);}
	
	/**  
	 * <b>isNull</b>
	 * <p>判断传入的对象是否属于空对象</p>
	 * @param param 传入的对象
	 * @return 是否为空
	 */ 
	public static boolean nonNull(Object param){
		return Optional.fromNullable(param).isPresent();
	}
	
	/**  
	 * <b>nonNull</b>
	 * <p>判断传入的对象是否属于空对象</p>
	 * @param param 传入的对象
	 * @return 是否为空
	 */ 
	public static boolean isNull(Object param){
		return !nonNull(param);
	}
	
	/**  
	 * <b>equals:校验对象是否相等</b>
	 * <p>底层可以采用的是guava的Objects对象操作</p>
	 * @param param1 对象1
	 * @param param2 对象2
	 * @return 是否相等
	 * @exception 无异常抛出
	 */ 
	public static boolean equals(Object param1,Object param2){
		return com.google.common.base.Objects.equal(param1, param2) /*&& java.util.Objects.equals(param1, param2)*/;
	}
		
	/**  
	 * <b>compareAndGet:如果第一个参数为空，就取第二个参数为获取值</b>
	 * <p>如果第一个参数为空，就取第二个参数为获取值</p>
	 * @param first
	 * @param defaultParam
	 * @return 获取最后非空的值
	 * @exception
	 */ 
	public static Object compareAndGet(Object first,Object defaultParam){
		return MoreObjects.firstNonNull(first, defaultParam);
	}
	
	/**  
	 * <b>getHashCode</b>
	 * <p>使用Google的hashcode算法实现开发hashCode犯法</p>
	 * @param objects
	 * @return
	 * @exception
	 */ 
	public static Object getHashCode(Object... objects){
		return com.google.common.base.Objects.hashCode(objects);
	}
	
	/**  
	 * <b>toStringHelper:执行返回ToStringHelper方法对象</b>
	 * <p>执行返回ToStringHelper方法对象</p>
	 * @param claxx clazz类
	 * @param containNullFlag 是否包含空 
	 * @return ToStringHelper
	 * @exception
	 */ 
	public static ToStringHelper toStringHelper(Class<?> claxx,boolean containNullFlag){
		ToStringHelper helper =MoreObjects.toStringHelper(claxx);
		helper = containNullFlag? helper.omitNullValues():helper;
		return helper;
	}
	
	/**  
	 * <b>toStringHelper:执行返回ToStringHelper方法对象</b>
	 * <p>执行返回ToStringHelper方法对象</p>
	 * @param obj 传入的toString对象
	 * @param containNullFlag 是否包含空 
	 * @return  ToStringHelper
	 * @exception
	 */ 
	public static ToStringHelper toStringHelper(Object obj,boolean containNullFlag){
		ToStringHelper helper =MoreObjects.toStringHelper(obj);
		helper = containNullFlag? helper.omitNullValues():helper;
		return helper;
	}
	
	/**  
	 * <b>toStringHelper:执行返回ToStringHelper方法对象</b>
	 * <p>执行返回ToStringHelper方法对象</p>
	 * @param className class类名称 
	 * @return ToStringHelper
	 * @exception
	 */ 
	public static ToStringHelper toStringHelper(Object obj){
		ToStringHelper helper =MoreObjects.toStringHelper(obj);
		return helper;
	}
	
	/**  
	 * <b>toStringHelper:执行返回ToStringHelper方法对象</b>
	 * <p>执行返回ToStringHelper方法对象</p>
	 * @param className class类名称 
	 * @param containNullFlag 是否包含空 
	 * @return ToStringHelper
	 * @exception
	 */ 
	public static ToStringHelper toStringHelper(String className,boolean containNullFlag){
		ToStringHelper helper =MoreObjects.toStringHelper(className);
		helper = containNullFlag? helper.omitNullValues():helper;
		return helper;
	}
	
	/**  
	 * <b>toString</b>
	 * <p>不用覆盖toString方法的工具toString方法</p>
	 * @param obj 传入的对象
	 * @return 打印的对象服务
	 * @exception
	 */ 
	public static String toString(Object obj,boolean containNullFlag){
		//判断校验是否为空
		if (!Optional.fromNullable(obj).isPresent()) {
			return "";
		}
		ToStringHelper helper = toStringHelper(obj,containNullFlag);
		Stream.of(obj.getClass().getDeclaredFields()).forEachOrdered(field->{
			if(!field.isAccessible()){
				field.setAccessible(true);
			}
			try {
				helper.add(field.getName(), field.get(obj));
			} catch (Exception e) {
				Throwables.propagate(e);
			}	
		});
		return Optional.fromNullable(helper.toString()).get();
	}
	
	/**  
	 * <b>toString</b>
	 * <p>不用覆盖toString方法的工具toString方法</p>
	 * @param obj 传入的对象
	 * @return 打印的对象服务
	 * @exception
	 */ 
	public static String toString(Object obj){
		//判断校验是否为空
		if (!Optional.fromNullable(obj).isPresent()) {
			return "";
		}
		ToStringHelper helper = toStringHelper(obj);
		Stream.of(obj.getClass().getDeclaredFields()).forEachOrdered(field->{
			if(!field.isAccessible()){
				field.setAccessible(true);
			}
			try {
				helper.add(field.getName(), field.get(obj));
			} catch (Exception e) {
				Throwables.propagate(e);
			}	
		});
		return Optional.fromNullable(helper.toString()).get();
	}
}
